/**************************************************************************
 * @doc Comm
 * @module syncdial.C 
 * Synchronous dialing task
 *
 * Product     :   
 * <nl>Developer   :   
 * <nl>Notes       :
 *
 * @head3 Comm Functions |
 * @index | Comm
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 * 
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form 
 * or by any means whether electronic, mechanical, magnetic, optical, 
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
$Log:
 * ------------------------------------------------------------------------
 ********************************************************************************/

#include <project.h>

#include <xmodem.h>
#include <ascii.h>
#include <acldev.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ACLStr.h>
#include <txoinc.h>
#include <svc.h>

#define DEBUG_SYNCTASK
#ifdef DEBUG_SYNCTASK
#define DEBUG_95
#endif

#include <mdmdefs.h>
#include <syncdial.h>
#include <spdebug.h>

int spipe;
int remotePipe;

/***************************************************************************** 
* @func int | main |
* 
* @rdesc return values.
* @flag void    |  
* @end
*****************************************************************************/

int main(int argc,const char* argv[]) {
    int inRetVal = 0;
    int hModem = -1;
    int inLocalTaskId = get_task_id();
    int remoteTaskID;
    int inOwnerTaskId = 0xFF;
    char szDevName[DEVICE_NAME_SIZE];
    DIALER_RESP srDialerResp;
    SYNC_DIAL_OBJECT obSyncTask;
    long ticks1,
    ticks2;

#ifdef DEBUG_SYNCTASK
    LOG_INIT("DIALER", LOGSYS_COMM, 0xFFFFFFFFL);
#endif

    /* OPen the pipe */
    spipe = open(DIALER_PIPE_ID, 0);
    pipe_init_msg(spipe, 3);

    //pdebug(("Init pipe %d",spipe));
    MEMCLR(szDevName, DEVICE_NAME_SIZE);
    strcpy(szDevName, SOFTPAY_PIPE_ID);
    remotePipe = get_owner(szDevName, &inOwnerTaskId); 
    pipe_connect(spipe, remotePipe); // connect pri & sec pipes

    while (1) {
        // Read the pipe message
        if (read(spipe, (char *) &obSyncTask, sizeof(SYNC_DIAL_OBJECT)) == 0) {
            // Wait for a pipe event
            pdebug(("DIALER Ready for EVENT"));
            while (wait_event() != EVT_PIPE) {
                //pdebug(("Wait for PIPE in Sync"));
            }
            continue;
        }

        // Got Data from the pipe
        if (obSyncTask.fAbort) {
            pdebug(("ABORTING..."));
            if (hModem > 0) {
                if (inGetModem(hModem, inLocalTaskId) != VS_SUCCESS)
                    pdebug(("MDM_DEV_CONFLICT,hModem=%d", hModem));
                inRetVal = inCloseModem(hModem);
                hModem = -1;
            }
        } else {
            pdebug(("Dialing %s", (obSyncTask.fAsync ? "ASYNC" : "SYNC")));
            ticks1 = read_ticks();
            if ((inRetVal = inDial(&hModem, &obSyncTask)) == HAYES_OK)
                inRetVal = inConnect(&hModem, spipe, obSyncTask.shBaudRate, !(obSyncTask.fAsync));

            //if ( inRetVal == MDM_DEV_ABORT )  REMARKED BY KELVIN (NO LINE AND CANCEL PRESSED)
            if (inRetVal == MDM_DEV_ABORT || (read(spipe, (char *) &obSyncTask, sizeof(SYNC_DIAL_OBJECT)) > 0)) {
                pdebug(("Aborting..."));
                if (hModem > 0)
                    inRetVal = inCloseModem(hModem);
                hModem = -1;
            } else {
                ticks2 = read_ticks();
                pdebug(("Dial Done Timing=%ld", (ticks2 - ticks1)));
                /* Once dialing is complete, 
                     Set the owner of the comms port back to primary task */
                remoteTaskID = atoi(argv[argc - 1]);
                set_owner(hModem, remoteTaskID);
                //pdebug (("Dial Result = %d", inRetVal));
                MEMCLR((char *) &srDialerResp, sizeof(DIALER_RESP));
                //kv1 debug 2001-10-15
                //srDialerResp.hModem = obSyncTask.hModem;
                srDialerResp.shMdmDialStatus = inRetVal;
                write(spipe, (char *) &srDialerResp, sizeof(DIALER_RESP)); 
                //kv1 debug 10/13/01
                //SVC_WAIT(80);
            }
        }
    } // end while(1)
    return (1);
} // end main
